# -*- coding: utf-8 -*-
# coding: utf-8
"""
处理文件上传的后端代码
"""

from flask import request
from flask_restful import Resource
import os
import uuid

# 获取当前文件的目录
BASE_DIR = os.path.dirname(os.path.abspath(__file__))

# 定义相对路径，并转换为绝对路径
UPLOAD_FOLDER = os.path.join(BASE_DIR, '../data')

# 确保目录存在
if not os.path.exists(UPLOAD_FOLDER):
    os.makedirs(UPLOAD_FOLDER)

# 入参：file_type:finetune_sets或question_sets，file_name

class FileUpload(Resource):
    def post(self):
        # 获取请求中的参数
        file_type = request.form.get('file_type')
        file_name = request.form.get('file_name')

        if not file_type or not file_name:
            return {"message": "file_type and file_name are required"}, 400

        if 'file' not in request.files:
            return {"message": "No file part"}, 400
        file = request.files['file']
        if file.filename == '':
            return {"message": "No selected file"}, 400

        # 创建文件保存路径
        file_path = os.path.join(UPLOAD_FOLDER, file_type, file_name)
        file.save(file_path)
        return {"message": "File uploaded successfully", "file_path": file_path, "file_name": file_name}, 200
